package com.clayecloud.module_house.ui;

import static com.scwang.smartrefresh.layout.util.DensityUtil.dp2px;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.clayecloud.module_house.R;
import com.clayecloud.module_house.adapter.ListViewDatabindingAdapter;
import com.clayecloud.module_house.databinding.ActivityHouseMainBinding;
import com.clayecloud.module_house.entity.House;
import com.clayecloud.module_house.viewmodels.ActivityHouseMainViewModel;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.bar.OnTitleBarListener;

import java.util.List;

@Route(path = "/house/index")
public class MainHouseActivity extends AppCompatActivity {

    private ActivityHouseMainBinding activityHouseMainBinding;
    private ActivityHouseMainViewModel activityHouseMainViewModel;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityHouseMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_house_main);
        //获取数据模型
        ViewModelProvider.Factory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication());
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        activityHouseMainViewModel = viewModelProvider.get(ActivityHouseMainViewModel.class);

        // 初始化函数进行初始化
        init();
        // 绑定监听事件
        initListener();
    }

    /**
     * 在删除此Activity时 进行操作防止内存泄漏
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.destroy(this, null);
    }

    /**
     * 刷新用
     */
    @Override
    public void onResume() {
        super.onResume();
        activityHouseMainViewModel.updateHouse();
    }
    /**
     * 初始化函数
     */
    private void init() {
        //将数据模型绑定到UI
        activityHouseMainViewModel.leadData();
        activityHouseMainBinding.setViewModel(activityHouseMainViewModel);
        // 控制状态栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        // 初始化滑动菜单栏
        activityHouseMainBinding.swipeMenuListView.setMenuCreator(new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
                deleteItem.setBackground(R.color.common_cancel_text_color);
                deleteItem.setWidth(dp2px(50));
                deleteItem.setTitle("删除");
                deleteItem.setTitleColor(R.color.common_black);
                deleteItem.setTitleSize(12);
                menu.addMenuItem(deleteItem);

            }
        });

        activityHouseMainViewModel.getHouseList().observe(this, new Observer<List<House>>() {
            @Override
            public void onChanged(List<House> houses) {
                ListViewDatabindingAdapter listViewDatabindingAdapter = new ListViewDatabindingAdapter(context, houses);
                activityHouseMainBinding.swipeMenuListView.setAdapter(listViewDatabindingAdapter);
            }
        });
    }

    /**
     * 绑定监听事件
     */
    public void initListener(){
        // 头部导航监听函数
        activityHouseMainBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View view) {
                finish();
                overridePendingTransition(R.anim.window_fake, R.anim.window_right_out);
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });
        //房屋滑动item监听函数
        activityHouseMainBinding.swipeMenuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index){
                    case 0:
                        activityHouseMainViewModel.deletehouse(position);
                        break;
                }
                return false;
            }
        });
        // 绑定房屋按钮点击监听
        activityHouseMainBinding.addHouseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance()
                        .build("/house/add/index")
                        .withTransition(R.anim.window_right_in, R.anim.window_fake)
                        .navigation(v.getContext());
            }
        });
    }
}